package com.yanqu.road.server.logic.activity.condition.peakstronghold;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakstronghold.PeakStrongHoldGuessValueArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class PeakStrongHoldGuessChampionCondition extends BaseActivityCondition {

    public PeakStrongHoldGuessChampionCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        PeakStrongHoldGuessValueArgs args = (PeakStrongHoldGuessValueArgs) object;

        Map<Integer,Integer> rightCountUserCountMap = args.getRightCountUserCountMap();
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        if(paramList.get(3).compareTo(args.getValue()) == 0){
            setValue(BigInteger.ONE);
        }

        //猜中人數變化了
        int paramValue = 0;
        if(rightCountUserCountMap.containsKey(paramList.get(3).intValue())){
            paramValue = rightCountUserCountMap.get(paramList.get(3).intValue());
        }
        if(StringUtils.isNullOrEmpty(getParam()) || new BigInteger(getParam()).compareTo(BigInteger.valueOf(paramValue)) != 0){
            setParam(paramValue + "");
            player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PeakStrongHoldGuessChampion.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PeakStrongHoldGuessChampion.getValue(), handle);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(BigInteger.ZERO) > 0;
    }
}
